package com.chj.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chj.constants.Constants;
import com.chj.entity.ECar;
import com.chj.entity.ECarModel;
import com.chj.entity.NetWork;
import com.chj.entity.Order;
import com.chj.entity.OrderModel;
import com.chj.entity.Park;
import com.chj.entity.ResultModel;
import com.chj.entity.User;
import com.chj.service.ECarService;
import com.chj.service.MessageService;
import com.chj.service.NetWorkServcie;
import com.chj.service.OrderService;
import com.chj.service.ParkService;
import com.chj.service.UserService;
import com.chj.util.JSONUtils;

@Controller
@RequestMapping("person")
public class PersonCenterController {

    @Resource
    private UserService userService;
    @Resource
    private ECarService eCarService;
    @Resource
    private OrderService orderService;
    @Resource
    private NetWorkServcie netWorkServcie;
    @Resource
    private ParkService ParkService;
    @Resource
    private MessageService messageService;

    //个人中心页面
    @RequestMapping(value="person_center")
    public ModelAndView personCenter(HttpServletRequest  request
                                   , HttpServletResponse response){

        User user = (User)request.getSession().getAttribute(Constants.LOGIN_USER);

        Map<String, Object> map = new HashMap<String, Object>();

        //个人信息
        map.put("user", user);

        return new ModelAndView("person/person_center",map);
    }

    //消息管理
    @RequestMapping("person_msg")
    public ModelAndView personMsg(HttpServletRequest  request
                                , HttpServletResponse response){
        return new ModelAndView("person/person_msg");
    }

    //订单管理
    @RequestMapping(value="person_order_manager")
    public ModelAndView personOrderManager(HttpServletRequest  request
                                         , HttpServletResponse response){

        return new ModelAndView("person/person_order_manager",null);
    }

    //我的电动车
    @RequestMapping(value="person_ecar")
    public ModelAndView personAddECar(HttpServletRequest  request
                                    , HttpServletResponse response){

        return new ModelAndView("person/person_ecar",null);
    }

    //充电站分享
    @RequestMapping(value = "network_share")
    public ModelAndView networkShare(HttpServletRequest  request
                                   , HttpServletResponse response){

        return new ModelAndView("person/network_share",null);
        
    }

    //个人资料更新
    @RequestMapping(value="person_update")
    public ModelAndView personUpdate(HttpServletRequest  request
                                   , HttpServletResponse response){

        return new ModelAndView("person/person_update",null);
    }
    
    //用户帮助
    @RequestMapping(value="person_help")
    public ModelAndView personHelp(HttpServletRequest  request
                                 , HttpServletResponse response){

    	return new ModelAndView("person/person_help",null);
    }

    @RequestMapping(value="person_update_password")
    @ResponseBody
    public JSONObject personUpdatePassword(HttpServletRequest  request
                                         , HttpServletResponse response
                                         , String userPassword){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        JSONObject jsonObject = new JSONObject();

        user.setUserPassword(userPassword);

        if (userService.changeUserPassword(user) > 0) {
            session.setAttribute(Constants.LOGIN_USER, user);

            jsonObject.put("flg", "true");
            messageService.sendMsg(user.getUserPk(), "用户密码修改成功");
        } else {
            jsonObject.put("flg", "false");
            jsonObject.put("errorMsg", "修改密码时候出现问题，请稍后再尝试！");
        }
        return jsonObject;
    }

    @RequestMapping(value ="person_update_user" )
    @ResponseBody
    public JSONObject personUpdateUser(HttpServletRequest  request
                                     , HttpServletResponse response
                                     , String userName
                                     , String userPhone
                                     , String userEmail){

        JSONObject result = new JSONObject();

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        user.setUserName(userName);
        user.setUserPhone(userPhone);
        user.setUserEmail(userEmail);
        user.setModifyDate(new Date());

        if (userService.changeUserData(user) > 0) {
            session.setAttribute(Constants.LOGIN_USER, user);

            result.put("flg", "true");
            messageService.sendMsg(user.getUserPk(), "用户信息修改成功！");
        } else {
            result.put("flg", "false");
            result.put("errorMsg", "修改密码时候出现问题，请稍后再尝试！");
        }

        return result;
    }

    //获取个人汽车列表进行操作
    @RequestMapping(value = "get_person_ecar")
    @ResponseBody
    public JSONObject getPersonEcar(HttpServletRequest  request
                                  , HttpServletResponse response){
        
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        JSONObject data = new JSONObject();

        List<ECarModel> eCarModelList = eCarService.getECarsByUserId(user.getUserPk());

        data.put("eCarModelList", eCarModelList);

        return JSONUtils.getJsonObject(data);
    }

    //获取订单信息
    @RequestMapping(value = "get_person_order")
    @ResponseBody
    public JSONObject getPersonOrder(HttpServletRequest  request
                                   , HttpServletResponse response){
        
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        JSONObject data = new JSONObject();

        List<Order> orderList = orderService.getOrderByUserIdByNow(user.getUserPk(),new Date());
        List<OrderModel> orderModelList = new ArrayList<OrderModel>();
        for (Order order : orderList) {
            OrderModel orderModel = new OrderModel();
            orderModel.setOrder(order);
            NetWork netWork = netWorkServcie.getNetWork(order.getNetworkId());
            netWork.setModifyDate(new Date());
            netWork.setDeleteDate(new Date());
            orderModel.setNetWork(netWork);
            Park park = ParkService.getParkById(order.getParkId());
            park.setModifyDate(new Date());
            park.setDeleteDate(new Date());
            orderModel.setPark(park);
            orderModelList.add(orderModel);
        }

        for (Order order : orderList) {
            order.setPayDate(new Date());
        }

        data.put("orderList", orderList);
        data.put("orderModelList", orderModelList);

        return JSONUtils.getJsonObject(data);
    }

    //获取订单信息
    @RequestMapping(value = "get_person_order_old")
    @ResponseBody
    public JSONObject getPersonOrderOld(HttpServletRequest  request
                                      , HttpServletResponse response){
        
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        JSONObject data = new JSONObject();

        List<Order> orderList = orderService.getOrderByUserIdByNowOver(user.getUserPk(),new Date());

        List<OrderModel> orderModelList = new ArrayList<OrderModel>();
        for (Order order : orderList) {
            OrderModel orderModel = new OrderModel();
            orderModel.setOrder(order);
            NetWork netWork = netWorkServcie.getNetWork(order.getNetworkId());
            netWork.setModifyDate(new Date());
            netWork.setDeleteDate(new Date());
            orderModel.setNetWork(netWork);
            Park park = ParkService.getParkById(order.getParkId());
            park.setModifyDate(new Date());
            park.setDeleteDate(new Date());
            orderModel.setPark(park);
            orderModelList.add(orderModel);
        }

        for (Order order : orderList) {
            order.setPayDate(new Date());
            
        }

        data.put("orderList", orderList);
        data.put("orderModelList", orderModelList);

        return JSONUtils.getJsonObject(data);
    }

    //添加充电汽车到个人账户下面去
    @RequestMapping(value = "add_person_ecar")
    @ResponseBody
    public JSONObject addPersonECar(HttpServletRequest  request
                                  , HttpServletResponse response
                                  , String ecarId
                                  , String ecarName
                                  , int    ecarTime
                                  , String ecarDescription
                                  , String ecarType){

        JSONObject result = new JSONObject();

        User user = (User)request.getSession().getAttribute(Constants.LOGIN_USER);

        ECar eCar = new ECar(ecarId, ecarName, ecarTime, ecarDescription, user.getUserPk());

        if ( eCarService.addECarToUser(eCar, user) > 0 ){

            result.put("flg", "true");
            messageService.sendMsg(user.getUserPk(), "添加电动车成功！");
        } else {

            result.put("flg", "false");
            result.put("errorMsg", "服务器出现问题添加失败，请稍后添加");
        }

        return result;
    }

    @RequestMapping(value="delete_person_ecar/{ecarId}")
    @ResponseBody
    public JSONObject deletePersonEcar(HttpServletRequest  request
                                     , HttpServletResponse response
                                     , @PathVariable("ecarId") String ecarId){

        JSONObject result = new JSONObject();

        ResultModel resultModel = eCarService.deletePersonECar(ecarId);

        if (resultModel.isFlg()) {
            result.put("flg", "true");
            User user = (User)request.getSession().getAttribute(Constants.LOGIN_USER);
            messageService.sendMsg(user.getUserPk(), "删除用户名下电动车！");
        } else {
            result.put("flg", "false");
            result.put("errorMsg", resultModel.getErrorMsg());
        }

        return result;
    }

    @RequestMapping(value="delete_person_order/{orderPk}")
    @ResponseBody
    public JSONObject deletePersonOrder(HttpServletRequest  request
                                     , HttpServletResponse response
                                     , @PathVariable("orderPk") String orderPk){

        JSONObject result = new JSONObject();

        ResultModel resultModel = orderService.deleteForOrder(orderPk);

        if (resultModel.isFlg()) {
            result.put("flg", "true");
            User user = (User)request.getSession().getAttribute(Constants.LOGIN_USER);
            messageService.sendMsg(user.getUserPk(), "删除一个订单成功！！！");
        } else {
            result.put("flg", "false");
            result.put("errorMsg", resultModel.getErrorMsg());
        }

        return result;
    }

    //获取订单信息
    @RequestMapping(value = "get_person_msg")
    @ResponseBody
    public JSONObject getPersonMsg(HttpServletRequest  request
                                   , HttpServletResponse response){

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.LOGIN_USER);

        JSONObject userQuery = messageService.userQuery(user.getUserPk(), new Date());

        return JSONUtils.getJsonObject(userQuery);
    }
}
